package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.Customer;
import cn.wolfcode.car.business.query.CustomerQueryObject;
import cn.wolfcode.car.business.service.ICustomerService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 岗位控制器
 */
@Controller
@RequestMapping("business/customer")
public class CustomerController {
    //模板前缀
    private static final String prefix = "business/customer/";

    @Autowired
    private ICustomerService customerService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:customer:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    @RequiresPermissions("business:customer:add")
    @RequestMapping("/addPage")
    public String addPage() {
        return prefix + "add";
    }


    @RequiresPermissions("business:customer:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model) {
        model.addAttribute("customer", customerService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:customer:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Customer> query(CustomerQueryObject qo) {
        return customerService.query(qo);
    }

    //新增
    @RequiresPermissions("business:customer:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Customer customer) {
        customerService.save(customer);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:customer:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Customer customer) {
        customerService.update(customer);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:customer:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        customerService.deleteBatch(ids);
        return AjaxResult.success();
    }

    //下载
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("客户列表");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("id");
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("年龄");
        row.createCell(3).setCellValue("电话");
        List<Customer> customerList = customerService.list();
        for (int i = 0; i < customerList.size(); i++) {
            Row row1 = sheet.createRow(i + 1);
            row1.createCell(0).setCellValue(customerList.get(i).getId());
            row1.createCell(1).setCellValue(customerList.get(i).getName());
            row1.createCell(2).setCellValue(customerList.get(i).getAge());
            row1.createCell(3).setCellValue(customerList.get(i).getPhone());

        }
        response.setHeader("Content-Disposition", "attachment; filename ="
                + java.net.URLEncoder.encode("客户名单.xlsx", "UTF-8"));
        //客户端浏览器输出流
        ServletOutputStream outputStream = response.getOutputStream();
        wb.write(outputStream);
        wb.close();
        outputStream.close();
    }

    @RequestMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(String phone) {
        Customer customer = customerService.getByPhone(phone);
        //JQuery Validata要求,存在返回1, 不存在返回0
        return customer == null ? "0" : "1";
    }





}
